// 引入字体样式 wangli
import "./common/iconfont/iconfont.css";
// 引入播放m3u8格式的视频插件
import "video.js/dist/video-js.css";
import "vue-video-player/src/custom-theme.css";
// 引入播放m3u8格式的视频插件
import "./common/reset.css";
import "./common/border.css";
import Vue from "vue";
import store from "./store";
Vue.use(store);
import App from "./App";
import router from "./router";
// import store from "./store";
import "amfe-flexible/index.js";
import Vant from "vant";
import "vant/lib/index.css";
// import { Dialog,Toast,Popup,Notify} from "vant";
import { Dialog ,Lazyload} from "vant";
Vue.use(Dialog);
// Vue.use(Toast);
// Vue.use(Popup);
// Vue.use(Notify);
Vue.use(Vant);
Vue.use(Lazyload);
import Bus from "./common/bus";
import directives from "./common/directives";
Vue.use(directives);
//
// import VueLazyload from "vue-lazyload";
// Vue.use(VueLazyload);
//2.在main.js里面引入插件
import VueWechatTitle from "vue-wechat-title"; //动态修改title
Vue.use(VueWechatTitle);

// import Header from "./mine/component/header";
// Vue.component("Header", Header);
import VideoPlayer from "vue-video-player";
Vue.use(VideoPlayer);
import "videojs-contrib-hls";
// 解决移动端点击300毫秒延迟问题
// import FastClick from "fastclick";
// FastClick.attach(document.body);
// FastClick.prototype.focus = function(targetElement) {
//   let length;
//   if (
//     targetElement.setSelectionRange &&
//     targetElement.type.indexOf("date") !== 0 &&
//     targetElement.type !== "time" &&
//     targetElement.type !== "month"
//   ) {
//     length = targetElement.value.length;
//     targetElement.focus();
//     targetElement.setSelectionRange(length, length);
//   } else {
//     targetElement.focus();
//   }
// };
// echarts 图表
import * as echarts from "echarts";
Vue.prototype.$echarts = echarts;
// 词云
// import * as wordcloud from "echarts-wordcloud";
// Vue.prototype.$wordcloud = wordcloud;
Vue.config.productionTip = false;
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
// 中国地图
import china from "echarts/map/json/china.json";
echarts.registerMap("china", china);
Vue.use(ElementUI, {
  zIndex: 300
});
//下拉加载
import infiniteScroll from "vue-infinite-scroll";
Vue.use(infiniteScroll);
import axios from "axios";
Vue.prototype.$axios = axios;

//请求发送拦截，把accountid带到header中
axios.interceptors.request.use(
  config => {
    //     个人收藏的接口
    config.headers.AccountId = localStorage.getItem("user_id");
    config.headers.Version = 2;
    config.headers.Platform = 1;  // 0：pc 1：H5 2：IOS 3：Android 
    /*     config.headers.AccountId = '21afb60a77cfffd866e95cc0ee79c535435d0bf77b54566c351c817eaee84b04a062b7adb55c73a66f5741051df3ab45'; */
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

import "babel-polyfill";
//正式的
// axios.defaults.baseURL = "https://api-dy-server.zsjx114.com/";
//测试的
// axios.defaults.baseURL = "https://test-api-dy-server.zsjx114.com/";

if (location.href.indexOf("://m-dy.zsjx114.com") > -1) {
  axios.defaults.baseURL = "https://api-dy-server.zsjx114.com/";
} else {
  axios.defaults.baseURL = "https://test-api-dy-server.zsjx114.com/";
}

// 引入一些会复用多次的公共方法 就不需要单独页面引入了
import {
  timestamp,
  format_num,
  fornum,
  format_time,
  formatRate,
  timestampToTime,
  trimStr,
  formatDate,
  formatDateNew,
  initDateInterval,
  urlPms,
  getStr,
  isWX,
  isZsjxApp,
  isMoblie,
  OSType,
  getCookie,
  levelName,
  levelVersions,
  getClientHeight,
} from "@/common/utils.js";
import { getPower } from "@/common/common.js";
Vue.prototype.$getPower = getPower;
Vue.prototype.urlPms = urlPms; //获取地址栏参数
Vue.prototype.timestamp = timestamp; //时间戳转换为时间
// Vue.prototype.na = 222;
Vue.prototype.format_num = format_num;
Vue.prototype.fornum = fornum;
Vue.prototype.format_time = format_time;
Vue.prototype.formatRate = formatRate;
Vue.prototype.formatDateNew = formatDateNew;
Vue.prototype.timestampToTime = timestampToTime;
Vue.prototype.trimStr = trimStr;
Vue.prototype.formatDate = formatDate;
Vue.prototype.initDateInterval = initDateInterval;
Vue.prototype.getStr = getStr;
Vue.prototype.$isZsjxApp = isZsjxApp;
Vue.prototype.$isMoblie = isMoblie;
Vue.prototype.$OSType = OSType;
Vue.prototype.$getCookie = getCookie;
Vue.prototype.$isWX = isWX();
Vue.prototype.$levelName = levelName;
Vue.prototype.$levelV = levelVersions;
Vue.prototype.$getClientHeight = getClientHeight;
Vue.prototype.$tabNavTopVal = '1.26rem';
//引入封装通用组件库
import components from "@/component";
Vue.use(components);

//引入会员弹出框组件
import vipPop from "@/component/vipPop";
Vue.use(vipPop);
// 全局注册我是有底线的
import FooterText from "@/component/FooterText";
Vue.component(FooterText.name, FooterText);
// 全局注册输入框自动获取焦点
Vue.directive("fofo", {
  inserted(el) {
    // 指令所在标签, 被插入到真实DOM触发一次(仅触发一次)
    fofo(el);
  },
  update(el) {
    // 指令所在标签被更新时触发
    fofo(el);
  }
});

function fofo(el) {
  if (el.nodeName === "INPUT" || el.nodeName === "TEXTAREA") {
    el.focus();
  } else {
    el.querySelector("input").focus();
  }
}
let appLoginCount = 0; //页面加载一次 只能调用一次客户端登录函数！
// 统一做相应拦截，如果是被挤掉的，直接跳对应的登录页
axios.interceptors.response.use(
  res => {
    if ((res.data.code == 1010 || res.data.code == 1006) && isZsjxApp) {
      console.log("登录异常码：" + res.data.code);
      //如果是客户端内打开，则调用客户端登录
      localStorage.removeItem("user_id");
      if (!appLoginCount) {
        appLoginCount++;
        console.log("开始调用客户端登录...");
        // window.appBridgeObj.checkLogin(JSON.stringify(res.data.code)); //调用客户端登录
        if (OSType == "ios") {
          //调用ios弹框方法
          window.webkit.messageHandlers.checkLogin.postMessage(
            JSON.stringify(res.data.code)
          );
        } else {
          //调用安卓弹框方法
          window.appBridgeObj.checkLogin(JSON.stringify(res.data.code));
        }
      }
      // res.isApp = true;
      return res;
    }
    let rPms = {
      path:'/login',
      query:{}
    }
    if(urlPms.invitecode){
      rPms.query.invitecode = urlPms.invitecode;
    }
    if (res.data.code == 1010) {
      //拦截被挤掉状态
      localStorage.removeItem("user_id");
      Dialog.confirm({
        title: "登录限制",
        message:
          "您的账号登录设备数已超出会员设备限制，请升级会员或重新登录使用"
      }).then(() => {
        // let routerObj = router.resolve({
        //   path: `/login`
        // });
        // location.href = routerObj.href;
        router.push(rPms);
      }).catch(() => {
        // let routerObj = router.resolve({
        //   path: `/login`
        // });
        // location.href = routerObj.href;
        router.push(rPms);
      });
    } else if (res.data.code == 1006) {
      //拦截未登录状态
      localStorage.removeItem("user_id"); //一定要清掉
      // if (isWX()) {
      //   //如果是微信端打开，则走App.vue的微信授权流程
      //   return res;
      // } else {
      //   // let routerObj = router.resolve({
      //   //   path: `/login`
      //   // });
      //   // location.href = routerObj.href;
      //   router.replace({ path: "/login" });
      // }
      router.replace(rPms);
    } else {
      return res;
    }
  },
  err => { }
);
new Vue({
  el: "#app",
  router,
  store,
  components: {
    App
  },
  template: "<App/>",
  data() {
    return {
      Bus
    };
  },
  watch: {
    $route() {
      let rName = this.$route.name;
      if (!(rName == "talent" || rName == "liveRank" || rName == "goods")) {
        setTimeout(() => {
          //hack - 解决ios点击返回页面白屏问题
          window.scrollTo(0, 0);
        }, 100);
      }
    }
  }
});
window.rm = function () {
  localStorage.removeItem("user_id");
};
